package com.yt.product.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yt.db.utils.RedisUtil;
import com.yt.product.models.vo.CategoryInfoTreeVO;
import com.yt.product.models.vo.CategoryProductInfoVO;
import com.yt.product.models.vo.ProductInfoVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.yt.product.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.product.mapper.CategoryInfoMapper;
import com.yt.product.models.service.ICategoryInfoService;
import com.yt.product.models.bo.CategoryInfoSaveBO;
import com.yt.product.models.dto.CategoryInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;

import static com.yt.common.utils.UtilConstants.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 分类信息控制器
 *
 * @author lvzeming
 * @Date 2025-08-11 16:05:22
 */
@DubboService
public class CategoryInfoServiceImpl extends ServiceImpl<CategoryInfoMapper, CategoryInfoDTO> implements ICategoryInfoService{

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public boolean add(CategoryInfoSaveBO saveBO) {
        CategoryInfoDTO dto = new CategoryInfoDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }


    @Override
    public List<CategoryInfoTreeVO> queryCategoryTree() {
        //从redis中取出数据
        List<CategoryInfoTreeVO> categoryInfoTreeVOList = this.getCategoryInfoTreeByRedis();
        if(null != categoryInfoTreeVOList && categoryInfoTreeVOList.size()>0){
            return categoryInfoTreeVOList;
        }
        //查询所有类目
        Wrapper<CategoryInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("status", ZERO);
        List<CategoryInfoDTO> list = this.selectList(wrapper);

        if (list != null && list.size() > 0) {
            List<CategoryInfoTreeVO> treeList = new ArrayList<>();
            for (CategoryInfoDTO dto : list) {
                CategoryInfoTreeVO treeVO = new CategoryInfoTreeVO();
                treeVO.setId(dto.getId());
                treeVO.setName(dto.getName());
                treeVO.setParentId(dto.getParentId());
                treeVO.setPic(dto.getPic());
                treeVO.setStatus(dto.getStatus());
                treeList.add(treeVO);
            }
            //建立树形结构
            List<CategoryInfoTreeVO> builTree = this.builTree(treeList);
            List<CategoryInfoTreeVO> realeList = new ArrayList<>();
            //商品加入到树形结构
            for (CategoryInfoTreeVO treeVO : builTree) {
                //查询商品信息
                List<ProductInfoVO> productInfoVOList = new ArrayList<>();
                for(CategoryInfoTreeVO child : treeVO.getChildList()){
                    List<ProductInfoVO> productInfoVOS = baseMapper.queryProductByCategoryId(child.getId());
                    productInfoVOList.addAll(productInfoVOS);
                }
                treeVO.setProductInfoVOList(productInfoVOList);
            }

            //保存buildTree到redis对象中
            this.saveCategoryInfoTreeToRedis(builTree);
            return builTree;
        }
        return null;
    }

    @Override
    public List<CategoryProductInfoVO> queryCategoryProductList() {
        //查询类目下所有商品信息
        List<CategoryProductInfoVO> categoryProductInfoVOList = baseMapper.queryCategoryProductList();
        return categoryProductInfoVOList;
    }

    //建立树形结构
    public List<CategoryInfoTreeVO> builTree(List<CategoryInfoTreeVO> menuList) {
        List<CategoryInfoTreeVO> treeMenus = new ArrayList<>();
        for (CategoryInfoTreeVO menuNode : getRootNode(menuList)) {
            menuNode = this.buildChilTree(menuNode, menuList);
            treeMenus.add(menuNode);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private CategoryInfoTreeVO buildChilTree(CategoryInfoTreeVO pNode, List<CategoryInfoTreeVO> menuList) {
        List<CategoryInfoTreeVO> chilMenus = new ArrayList<>();
        for (CategoryInfoTreeVO menuNode : menuList) {
            if (menuNode.getParentId().equals(pNode.getId())) {
                chilMenus.add(this.buildChilTree(menuNode, menuList));
            }
        }
        pNode.setChildList(chilMenus);
        return pNode;
    }

    //获取根节点
    private List<CategoryInfoTreeVO> getRootNode(List<CategoryInfoTreeVO> menuList) {
        List<CategoryInfoTreeVO> rootMenuLists = new ArrayList<>();
        for (CategoryInfoTreeVO menuNode : menuList) {
            if (menuNode.getParentId().intValue() == 0) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }

    //从redis中获取json格式的类目数据
    private List<CategoryInfoTreeVO> getCategoryInfoTreeByRedis(){
        //从redis获取CATEGORY_REDIS_KEY数据
        Object o = redisUtil.get(CATEGORY_REDIS_KEY);
        //转换redis中的数据为对象数据
        List<CategoryInfoTreeVO> categoryInfoTreeVOList = JSONObject.parseArray(JSONObject.toJSONString(o),CategoryInfoTreeVO.class);
        return categoryInfoTreeVOList;
    }

    //保存CategoryInfoTree到redis中
    private void saveCategoryInfoTreeToRedis(List<CategoryInfoTreeVO> categoryInfoTreeVOS){
        Object json = JSONObject.toJSON(categoryInfoTreeVOS);
        redisUtil.set(CATEGORY_REDIS_KEY,json);
    }
}
